package at.ac.tuwien.sepm.s2011.e0208329.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import at.ac.tuwien.sepm.s2011.e0208329.db.DatabaseManager;
//import at.ac.tuwien.sepm.s2011.e0208329.domain.Dienstleistung;
import at.ac.tuwien.sepm.s2011.e0208329.domain.Rechnung;

public class RechnungDAOImpl implements RechnungDAO {// support the database-access for table "Rechnung"
	
	private static final Logger log = Logger.getLogger(RechnungDAOImpl.class);
	private DatabaseManager dm; 
	
	public RechnungDAOImpl() {
		this.dm = DatabaseManager.getDatabaseManager(); // open the Connection to my database
	}

	@Override
	public void insertBill(Rechnung bill) throws SQLException{// insert a new Bill to the Database, exclusive order the Services
		if (bill == null) { // Check the Parameter
			throw new IllegalArgumentException();
		}
		String query = "INSERT INTO rechnung(Name, Adresse) VALUES ('"+bill.getCustomerName()+"','"+bill.getAddress()+"')"; //SQL-Command
		log.debug(query);// output the SQL-Command as debug info
		dm.update(query); // run the method from DatabaseManager to perform the Update-Service		
	}

	@Override
	public ArrayList<Rechnung> getAllBills() throws SQLException {// Perform the "select * from rechnung" and return as a List
		String query = "SELECT * FROM rechnung"; //SQL-Command
		ArrayList<ArrayList<String>> rsArray = dm.query(query); //run the method from DatabaseManager to perform the query-Service
		ArrayList<Rechnung> bills = new ArrayList<Rechnung>();
		for (ArrayList<String> arrl : rsArray) {  // change the data type to Bill and add it to the to be returned list
			int id = Integer.parseInt(arrl.get(0));
			String cusName = arrl.get(1);
			String cusAdress = arrl.get(2);
			Rechnung myBill = new Rechnung(cusName, cusAdress);
			myBill.setIdBill(id);
			bills.add(myBill);
		}
		return bills;
	}

	@Override
	public Rechnung getBill(int idBill) { // it's function has been replaced from getAllServieceIDforBill()
//		String query = "select * from rechnung where idrechnung="+idBill;
//		ArrayList<ArrayList<String>> rsArray = dm.query(query);
//		ArrayList<Rechnung> bills = new ArrayList<Rechnung>();
//		for (ArrayList<String> arrl : rsArray) {
//			int id = Integer.parseInt(arrl.get(0));
//			String cusName = arrl.get(1);
//			String cusAdress = arrl.get(2);
//			Rechnung myBill = new Rechnung(cusName, cusAdress);
//			myBill.setIdBill(id);
//			bills.add(myBill);
//		}
		return null;
	}

	@Override
	public boolean order(Map<String, Integer> orderList) {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * get all Service's ID and the amount from table 'rechnungitem' which has the same Bill ID
	 * actually it should not be here, but in the OrderDAOImpl, but for convenience I set it here, 
	 * because when you need to show a bill, you need this method
	 */
	public Map<Integer, Integer> getAllServiceIDForBill(int billID) throws SQLException {
		String query = "select fk_dienstleistung,sum(anzahl) from rechnungitem where fk_rechnung="
				+ billID + " group by fk_dienstleistung";
		ArrayList<ArrayList<String>> rsArray = dm.query(query);
		Map<Integer, Integer> ser_num = new HashMap<Integer, Integer>();
		for (ArrayList<String> arrl : rsArray) {
			Integer serviceID = Integer.valueOf(arrl.get(0));
			Integer number = Integer.valueOf(arrl.get(1));
			ser_num.put(serviceID, number);
		}
		return ser_num;
	}

}
